/*
 * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.net.dreamlu.net).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package netty.mqtt;

import com.google.gson.Gson;
import net.dreamlu.iot.mqtt.codec.ByteBufferUtil;
import net.dreamlu.iot.mqtt.core.client.MqttClient;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

public class FryDeviceBtn {

    private static final Logger logger = LoggerFactory.getLogger(FryDeviceBtn.class);

    public static void main(String[] args) throws IOException {
        AtomicInteger count = new AtomicInteger();
        // 初始化 mqtt 客户端
        MqttClient client = MqttClient.create()
                .ip("127.0.0.1")
                .port(1883)
                .connect();
//        String pushCmd = FileUtils.readFileToString(new File("D:\\spring-demo\\tool-example\\src\\main\\java\\netty\\mqtt\\fry_program_send.json"),
//                Charset.defaultCharset());

        String pushCmd = FileUtils.readFileToString(new File("D:\\spring-demo\\tool-example\\src\\main\\java\\netty\\mqtt\\fry_menu_send.json"),
                Charset.defaultCharset());


        client.publish("/api/device/notify", ByteBuffer.wrap(pushCmd.getBytes()));

        client.subQos0("/us/qpm/client/config", (topic, payload) -> {
            String json = ByteBufferUtil.toString(payload);
            logger.info(topic + '\t' + json);
            FryingSetKeyBean fryingSetKey = new Gson().fromJson(json, FryingSetKeyBean.class);

            FryingSetKeyBean ack = new FryingSetKeyBean();
            ack.setGuid(UUID.randomUUID().toString().replaceAll("-", "").substring(0, 16));
            ack.setSource(fryingSetKey.getDestination());
            ack.setDestination(fryingSetKey.getSource());
            ack.setType("WRITE_RECIPE_ACK");
            ack.setCorrelationId(fryingSetKey.getGuid());
            client.publish("/us/qpm/server/config", ByteBuffer.wrap(new Gson().toJson(ack).getBytes()));
            int index = count.getAndIncrement();
            System.out.println(index);
            if (index % 2 == 0) {
                ack.setGuid(UUID.randomUUID().toString().replaceAll("-", "").substring(0, 16));
                ack.setType("WRITE_RECIPE_SUCCESS");
                logger.info("{}发送WRITE_RECIPE_SUCCESS", index);
                client.publish("/us/qpm/server/config", ByteBuffer.wrap(new Gson().toJson(ack).getBytes()));
            }
        });
    }

}
